package com.zjl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjl.common.R;
import com.zjl.dto.DishDto;
import com.zjl.entity.Dish;
import com.zjl.entity.DishFlavor;
import com.zjl.mapper.DishMapper;
import com.zjl.service.DishFlavorService;
import com.zjl.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 在Dish表新增菜品信息，在DishFlavor新增对应的口味信息
     * @param dishDto
     */
    @Override
    @Transactional//涉及多张表，开启事务控制，在启动类也需要加注解
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息,由于DishDto继承Dish，所以直接save行得通。
        this.save(dishDto);

        //save保存后dishDto会得到数据库分配的id
        Long dishId = dishDto.getId();

        //将dishId一一赋给flavors，没有dishId，DishFlvor保存没有意义
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }

        //保存DishFlavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     *根据id查询菜品信息，并查询该菜品的口味信息
     * @param dishId
     */
    @Override
    public DishDto getByIdWithFlavor(Long dishId) {
        //获得当前菜品信息
        Dish dish = this.getById(dishId);

        //查询当前菜品id关联的DishFlavor
            //条件构造器
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            //添加条件
        wrapper.eq(dishId!=null,DishFlavor::getDishId,dishId);

        //对DishFlavor表进行查询
        List<DishFlavor> flavors = dishFlavorService.list(wrapper);

        //新建DishDto对象
        DishDto dishDto = new DishDto();

        //为DishDto对象赋值
        dishDto.setFlavors(flavors);
        BeanUtils.copyProperties(dish,dishDto);

        return dishDto;
    }


    /**
     * 修改菜品信息，再修改口味表信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //根据DishId修改Dish表数据
        this.updateById(dishDto);

        //修改当前菜品对应口味信息
            //删除当前菜品对应的旧口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);

            //DishFlavor表在新增dishDto中的Flavors
                //细节：dishDto对象中的Flavors列表内的每个DishFlavor对象的DishId属性值为空，需要手动设置才能保存
                    //设置DishId
        List<DishFlavor> flavors = dishDto.getFlavors();
        Long dishId = dishDto.getId();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }

                //插入当前菜品新的口味信息
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据条件查询菜品
     * @param dish
     * @return
     */
    @Override
    public List<DishDto> getWithFlavors(Dish dish) {

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        //细节:status=1（菜品启售中）才进行查询。
        wrapper.eq(Dish::getStatus,1);

        //增加categoryId条件
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());

        //添加name条件
        wrapper.like((dish.getName()!=null&&dish.getName()!=""),Dish::getName,dish.getName());

        //添加排序条件
        wrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> dishes = this.list(wrapper);
        List<DishDto> dishDtos=new ArrayList<>();


        for (Dish dish1 : dishes) {
            //查询每个菜品对应的口味
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,dish1.getId());
            List<DishFlavor> flavors = dishFlavorService.list(wrapper1);

            //新建DishDto对象
            DishDto dishDto = new DishDto();

            //为DishDto对象的flavors属性赋值
            dishDto.setFlavors(flavors);

            //属性值拷贝
            BeanUtils.copyProperties(dish1,dishDto);

            //dishDto添加到队列
            dishDtos.add(dishDto);
        }

        return dishDtos;
    }
}
